package alg20250316;

public class Text2 {
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        int sum = 0;
        //1 创建表
        int f[] = new int[n + 1];
        int g[] = new int[n + 1];
        //2 初始化

        //3 填表
        for (int i = 1; i <= n; i++) {
            f[i] = Math.max(nums[i - 1], nums[i - 1] + f[i - 1]);
            g[i] = Math.min(nums[i - 1], nums[i - 1] + g[i - 1]);
            sum += nums[i - 1];
        }
        int max = f[1], min = g[1];
        for (int i = 2; i <= n; i++) {
            if(max < f[i]) max = f[i];
            if(min > g[i]) min = g[i];
        }
        //4 返回值
        return sum == min ? max : Math.max(max, sum - min);


    }
}
